<template>
	<view class="contents">
		<view class="" style="border-left: 3px solid #1262DB;padding-left: 20rpx;font-weight: bold;">
			已有设备
		</view>
		
		<view class="" v-for="(item,index) in datalist" style="padding: 20rpx;border-radius: 20rpx;box-shadow: 2px 2px 5px 5px #ececec;margin-top: 30rpx;display: flex;flex-direction: column;">
			<view class="" style="display: flex;justify-content: space-between;align-items: center;">
				<view class="">
					状态：<text style="color: green;" v-if="item.is_online == 1">在线</text><text style="color: #666666;" v-else>离线</text>
				</view>
				<view class="">
					<switch :disabled="true" :checked="item.is_online == 1 ? true : false" color="#FFCC33" style="transform:scale(0.7)"/>
				</view>
			</view>
			<view class="xian">
				
			</view>
			<view class="" style="margin-top: 10rpx;color: #909090;font-size: 28rpx;">
				设备编号:{{item.device_num}}
			</view>
			<view class="" style="margin-top: 10rpx;color: #909090;font-size: 28rpx;">
				<view class="">
					C级业务员:{{item.cid_text}}
				</view>
				<view class="">
					
				</view>
			</view>
			<view class="" style="margin-top: 10rpx;color: #909090;font-size: 28rpx;display: flex;justify-content: space-between;align-items: center;">
				<view class="">
					B级业务员:{{item.bid_text}}
				</view>
				<view class="bts" v-if="group_id == 1"  @click="binduser(index)">
					绑定业务员
				</view>
				
			</view>
			<view class="" style="margin-top: 10rpx;color: #909090;font-size: 28rpx;display: flex;justify-content: space-between;align-items: center;">
				
				<view class="">
					A级业务员:{{item.aid_text}}
				</view>
				<view class="bts" v-if="group_id == 2" @click="binduser(index)">
					绑定业务员
				</view>
			</view>
			
			<view class="" style="margin-top: 10rpx;color: #909090;font-size: 28rpx;display: flex;justify-content: space-between;align-items: center;">
				
				<view class="">
					诊所:{{item.clinic_text}}
				</view>
				<view class="bts" v-if="group_id == 3" @click="bindclinic(index)">
					绑定诊所
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				datalist:[],
				group_id:''
			}
		},
		onShow() {
			this.getdata()
		},
		
		onLoad() {
			// this.getdata()
			this.getGroupId()
		},
		methods: {
			bindclinic(index){
				let that = this
				if(this.datalist[index]['clinic_id'] != 0){
					uni.showModal({
							title: '提示',
							content: '该设备已经绑定诊所，如更换诊所将导致原诊所无法使用该设备，请慎重操作？',
							success: function(res) {
							if (res.confirm) {
							    // 执行确认后的操作
								uni.navigateTo({
									url:'/pages/personalCenter/bindclinic?device_no='+that.datalist[index]['device_num']+"&id="+that.datalist[index]['id']
								})
							} 
							else {
								// 执行取消后的操作
							}
						}
					})
				}else{
					uni.navigateTo({
						url:'/pages/personalCenter/bindclinic?device_no='+that.datalist[index]['device_num']+"&id="+that.datalist[index]['id']
					})
				}
			},
			binduser(index){
				let that = this
				if(this.datalist[index]['clinic_id'] != 0){
					uni.showModal({
							title: '提示',
							content: '该设备已经绑定诊所，如更换代理商将重置该设备已绑定的诊所导致诊所设备无法使用，请慎重操作？',
							success: function(res) {
							if (res.confirm) {
							    // 执行确认后的操作
								uni.navigateTo({
									url:'/pages/personalCenter/binduser?device_no='+that.datalist[index]['device_num']
								})
							} 
							else {
								// 执行取消后的操作
							}
						}
					})
				}else{
					uni.navigateTo({
						url:'/pages/personalCenter/binduser?device_no='+that.datalist[index]['device_num']
					})
				}
				// console.log(this.datalist[index])
				
			},
			getGroupId(){
				var that = this
				uni.request({
				    url: that.global.url + 'api/user/getuserinfo',
				    method: "POST",
					header: {
						'token': uni.getStorageSync('token')
					},
				    data: {},
				    success(res) {
				        let result = res.data
				        if(result.code == 1){
							that.group_id = result.data.group_id
							console.log(result.data.group_id)
						}else{
				           uni.showToast({
				           	title: result.msg,
				           	icon: 'error'
				           })
				        }
				       
				    }
				})
			},
			getdata(){
				var that = this
				uni.showLoading({
					title: '加载中...'
				})
				uni.request({
					url: this.global.url + 'api/allocation/index',
					method: 'POST',
					header:{'token':uni.getStorageSync('token')},
					data: {
						// clinic_id: uni.getStorageSync('token')
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							// 存用户信息
							that.datalist = result.data
							
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none'
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			
		}
	}
</script>

<style>
.contents{
	padding: 30rpx;
}
.xian{
	border-bottom: 1px solid #ececec;
}
.bts{
	padding: 15rpx;
	border-radius: 10rpx;
	border: 1px solid #0000ff;
	color: #0000ff;
	font-size: 20rpx;
}
</style>
